<?php

namespace App\Admin\Controllers;

use App\Services\AiAppService;
use Illuminate\Support\Str;
use Slowlyo\OwlAdmin\Controllers\AdminController;

/**
 * 客户端管理
 *
 * @property AiAppService $service
 */
class AiAppController extends AdminController
{
	protected string $serviceName = AiAppService::class;

	public function list()
	{
		$crud = $this->baseCRUD()
			->filter($this->baseFilter()->body([
				amis()->TextControl('app_key', '应用Key')->size('md')->clearable(1),
			]))
			->headerToolbar([
				$this->createButton('dialog'),
				...$this->baseHeaderToolBar()
			])
			->columns([
				amis()->TableColumn('id', 'ID')->sortable(),
                amis()->TableColumn('description', '备注'),
				amis()->TableColumn('app_key', '应用Key'),
				amis()->TableColumn('expire_at', '有效期'),
				amis()->TableColumn('created_at', admin_trans('admin.created_at'))->type('datetime')->sortable(),
				$this->rowActions('dialog')
			]);

		return $this->baseList($crud);
	}

	public function form($isEdit = false)
	{
        $app_key = $isEdit ? '' : Str::random();
        $app_secret = $isEdit ? '' : Str::random(32);
		return $this->baseForm()->body([
			amis()->TextControl('app_key', '应用Key')->required(1)->readOnly(1)->value($app_key),
			amis()->TextControl('app_secret', '应用秘钥')->required(1)->readOnly(1)->value($app_secret),
			amis()->DateTimeControl('expire_at', '有效期')->required(1)->format('YYYY-MM-DD HH:mm:ss'),
            amis()->TextControl('description', '备注'),
            amis()->TextControl('image_model', '图片模型'),
            amis()->TextareaControl('image_prompt', '图片提示词'),
            amis()->TextareaControl('prompt_tpl', '组合提示词模板'),
            amis()->TextControl('plus_token', '普通版剩余token'),
            amis()->TextControl('max_token', '高级版剩余token'),
            amis()->TextControl('image_token', '图片剩余token'),
		]);
	}

	public function detail()
	{
		return $this->baseDetail()->body([
			amis()->TextControl('id', 'ID')->static(),
			amis()->TextControl('app_key', '应用Key')->static(),
			amis()->TextControl('app_secret', '应用秘钥')->static(),
			amis()->TextControl('expire_at', '有效期')->static(),
			amis()->TextControl('description', '备注')->static(),
			amis()->TextControl('token', '剩余token')->static(),
            amis()->TextControl('plus_token', '普通版剩余token')->static(),
            amis()->TextControl('used_plus_token', '普通版已用token')->static(),
            amis()->TextControl('max_token', '高级版剩余token')->static(),
            amis()->TextControl('used_max_token', '高级版已用token')->static(),
            amis()->TextControl('image_token', '图片剩余token')->static(),
            amis()->TextControl('used_image_token', '图片已用token')->static(),
			//amis()->TextControl('created_at', admin_trans('admin.created_at'))->static(),
			//amis()->TextControl('updated_at', admin_trans('admin.updated_at'))->static(),
		]);
	}
}
